package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.StrongBoxBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.util.WsUtils;
import com.panda.pandawash.view.SetSafeDialog;
import com.panda.pandawash.view.StatusBarCompat;

import org.xutils.x;

/**
 * Created by gaowf on 2017/3/26.
 */

public class SetSafeActivity extends Activity {
    private String TAG = "SetSafeActivity";

    private Button cancelButton;
    private Button confirmButton;
    private EditText firstEditText;
    private EditText confirmEditText;

    boolean setSuccess = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setFinishOnTouchOutside(false);
        setContentView(R.layout.dialog_set_safe);
        x.view().inject(this);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }

        firstEditText = (EditText) findViewById(R.id.first_psd);
        confirmEditText = (EditText) findViewById(R.id.confirm_first_psd);

        cancelButton = (Button) findViewById(R.id.set_psd_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("gwf_add", "click cancel in dialog");
            finish();
            }
        });

        confirmButton = (Button) findViewById(R.id.set_psd_confirm);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                comparePassword();
                   /* if (setSuccess == true) {
                        //final View progress = inflater.inflate(R.layout.dialog_progress, null);
                        //dialog.setContentView(progress);
                        //这里设置成功跳转界面或者其他逻辑
                        //TODO
                    } else {
                        //TODO
                    }*/
            }
        });

    }
    protected void comparePassword() {
        String password1 = firstEditText.getText().toString().trim();
        String password2 = confirmEditText.getText().toString().trim();
        Log.d("hys123","getInputPsd "+ password1 +" getConfirmPsd " + password2);

        if (!WsUtils.isMatch(WsUtils.REGEX_STRONGPASSWORD, password1)) {
            showMessage(getResources().getString(R.string.safe_box_password_legal));
            setSuccess = false;
            return;
        }

        if (password1.equals(password2) && password2 != null && password1 != null) {
            setSuccess = true;
            setupStrongBoxPsw();

        } else {
            showMessage(getResources().getString(R.string.registe_password_not_same));
            setSuccess = false;
        }
    }

    private void showMessage(String string) {
        Toast toast = Toast.makeText(SetSafeActivity.this, string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    private void setupStrongBoxPsw(){
        String account = CDApplication.getIntance().getUserInfo().getUseraccount();
        String password = firstEditText.getText().toString().trim();
        StrongBoxBean strongBoxBean = new StrongBoxBean();
        strongBoxBean.setSecretpassword(password);
        HttpUtil.setupStrongBoxPSW(new SafeBoxSetupCallback(),account,account,strongBoxBean);
    }

    private class SafeBoxSetupCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            Log.d(TAG, "SafeBoxSetupCallback statusCode = " + statusCode);
            Log.d(TAG, "SafeBoxSetupCallback onSuccess = " + content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, null);
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if(code.equals("0")){
                        showMessage(getResources().getString(R.string.safe_box_password_setup_success));
                        Intent intent = new Intent(SetSafeActivity.this, SafeBoxActivity.class);
                        startActivity(intent);
                        finish();
                    }else if(code.equals("99")){
                        showMessage(message);
                    }
                }
            } else {
                showMessage(getResources().getString(R.string.safe_box_password_setup_fail));
            }
            finish();
        }

        @Override
        public void onFailure(Throwable error, String content) {
            super.onFailure(error, content);

            Log.d(TAG, "onFailure = " + error);
            if (error.toString().contains("refused")) {

            }
            finish();
        }
    }

}

